<div>
  <h1><%= @category.new_record? ? "新建分类" : "修改分类 ##{params[:id]}" %></h1>
</div>

<div class="form-body">
  <%= form_for @category,
    url: (@category.new_record? ? admin_categories_path : admin_category_path(@category)),
    method: (@category.new_record? ? 'post' : 'put'),
    html: { class: 'form-horizontal' } do |f| %>

    <% unless @category.errors.blank? %>
      <div class="alert alert-danger">
        <ul class="list-unstyled">
          <% @category.errors.messages.values.flatten.each do |error| %>
            <li><i class="fa fa-exclamation-circle"></i> <%= error %></li>
          <% end -%>
        </ul>
      </div>
    <% end -%>

    <div class="form-group">
      <label for="ancestry" class="col-sm-2 control-label">所属分类:</label>
      <div class="col-sm-5">
        <select name="category[ancestry]">
          <option></option>
          <% @root_categories.each do |category| %>
            <% next if category == @category %>
            <option value="<%= category.id %>" <%= @category.ancestry == category.id.to_s ? 'selected' : '' %>><%= category.title %></option>
          <% end -%>
        </select>
        为空为一级分类
      </div>
    </div>
    <div class="form-group">
      <label for="title" class="col-sm-2 control-label">名称:*</label>
      <div class="col-sm-5">
        <%= f.text_field :title, class: "form-control" %>
      </div>
    </div>
    <div class="form-group">
      <label for="weight" class="col-sm-2 control-label">权重:</label>
      <div class="col-sm-5">
        <%= f.text_field :weight, class: "form-control" %> 数值越大越靠前
      </div>
    </div>
    <div class="form-group">
      <label for="icon" class="col-sm-2 control-label">Icon:</label>
      <div class="col-sm-5">
        <%= f.text_field :icon, class: "form-control" %> icon名
      </div>
    </div>
    <div class="form-group">
      <div class="col-sm-offset-2 col-sm-8">
        <%= f.submit (@category.new_record? ? "新建分类" : "编辑分类"), class: "btn btn-primary" %>
      </div>
    </div>
  <% end -%>
</div>
